package cn.itsource.xhy.org.controller;

import cn.itsource.xhy.org.domain.Shop;
import cn.itsource.xhy.org.domain.Shop;
import cn.itsource.xhy.org.domain.ShopLog;
import cn.itsource.xhy.org.query.ShopQuery;
import cn.itsource.xhy.org.service.IShopService;
import cn.itsource.xhy.org.service.IShopService;
import cn.itsource.xhy.utils.AjaxResult;
import cn.itsource.xhy.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private IShopService shopService;

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

    @PostMapping("/setShop")
    public AjaxResult setShop(@RequestBody Shop shop){
        try {
            shopService.setShop(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

    @PutMapping // put请求添加和修改，在参数中加上@RequestBody注解接收json格式数据，转换成对象
    public AjaxResult saveOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.save(shop);
            } else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

    @DeleteMapping("/{id}") // delete请求，删除，注解后面加上("/{id}")，地址栏中的占位符，  在参数中加上@PathVariable("id")，传递id
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败" + e.getMessage());
        }
    }

    @GetMapping("/{id}") // get请求，查单个，注解后面加上("/{id}")，地址栏中的占位符，   在参数中加上@PathVariable("id")，传递id
    public Shop findById(@PathVariable("id") Long id){
        try {
            return shopService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @GetMapping  // get请求，查所有
    public List<Shop> findAll(){
        try {
            return shopService.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    public QueryResult<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    @PostMapping("/reject")
    public AjaxResult reject (@RequestBody ShopLog shopLog){
        try {
            shopService.reject(shopLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/pass")
    public AjaxResult pass (@RequestBody ShopLog shopLog){
        try {
            shopService.pass(shopLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            shopService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败" + e.getMessage());
        }
    }

}
